package com.ddsfec.homepageapi.service;

import com.google.gson.Gson;
import org.apache.solr.client.solrj.SolrClient;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocumentList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class SolrServiceImpl {

    @Autowired
    private SolrClient solrClient;


    private Gson gson = new Gson();


    public SolrDocumentList list(String goodsName) {
        SolrDocumentList results = null;
        try {
            SolrQuery solrQuery = new SolrQuery("goodsName:" + goodsName);
            //设置查询的条数用于分页
            solrQuery.setRows(10);
            //设置查询的开始用于分页
            solrQuery.setStart(0);

            //设置高亮
            //solrQuery.setHighlight(true);
            //solrQuery.addHighlightField("goodsName");
            //solrQuery.setHighlightSimplePre("<label style='yellow'>");
            //solrQuery.setHighlightSimplePost("</label>");
            //执行查询
            QueryResponse queryResponse = solrClient.query(solrQuery);
            //返回高亮显示结果
            //Map<String, Map<String, List<String>>> highlighting = queryResponse.getHighlighting();

            //遍历查询结果（每个SolrDocument对象就表示一行查询记录）
            results = queryResponse.getResults();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return results;
    }

//    /**
//     * 查询
//     * @param search
//     * @param type
//     */
//    public List<Goods.GoodsSolr> getGoodsBySearch(String search, String type,String page,String level, String toped,String recomed) throws Exception {
//        List<Goods.GoodsSolr> Goods =new ArrayList<>();
//        try{
//
//            //SolrQuery solrQuery = new SolrQuery("goodsName:" + SolrUtils.checkNull(search)+" and typeId:"+SolrUtils.checkNull(type));
//            SolrQuery solrQuery=new SolrQuery();
//
//            solrQuery.set("q"
//                    ,"*:*"
//            );
//            solrQuery.set("fq"
//           ,"goodsName:"+SolrUtils.checkNull(search)
//                    ,"typeId:"+SolrUtils.checkNull(type)
//                    ,"level:"+SolrUtils.checkNull(level)
////                    ,"toped:"+SolrUtils.checkNull(toped)
//                    ,"recomed:"+SolrUtils.checkNull(recomed)
//            );
//            if(toped.equals("1")){
//                solrQuery.setSort( "orderNo" , SolrQuery.ORDER.desc);
//            }
//
//
//            //设置查询的条数用于分页
//            solrQuery.setRows(10);
//
//
//            //执行查询
//            QueryResponse queryResponse = solrClient.query(solrQuery);
//
//            SolrDocumentList results = queryResponse.getResults();
//
//            String solrJson = gson.toJson(results);
//            System.out.println(solrJson);
//
//            Goods = gson.fromJson(solrJson,new TypeToken<List<Goods.GoodsSolr>>(){}.getType());
//
//        }catch (Exception e){
//            e.printStackTrace();
//            throw new Exception("solr service fail 82");
//        }
//        return Goods;
//    }
}
